<?php
namespace app\member\model;

use app\BaseModel;
use app\shop\model\IndexShopOrderGoods;
use think\facade\Db;

class Goods extends BaseModel
{
    //会员商品表
    protected $name = 'longbing_card_member_goods';



    /**
     * @author chenniang
     * @DataTime: 2020-07-15 09:49
     * @功能说明:添加
     */
    public function goodsAdd($data){

        $res = $this->insert($data);

        return $res;
    }




    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:08
     * @功能说明:详情
     */
    public function levelInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @param $order
     * @功能说明:会员商品支付回调
     * @author chenniang
     * @DataTime: 2020-07-29 15:59
     */
    public function payResult($order){

        if($order['is_member']==1){

            $order_goods_model = new IndexShopOrderGoods();

            $order_item = $order_goods_model->orderGoodsInfo([ 'order_id' => $order[ 'id' ] ]);

            foreach ( $order_item as $index => $item ) {

                if($item['is_member']==1){

                    $this->getGrowth($item['goods_id'],$order['id'],$order['uniacid'],$order['user_id']);
                }

            }

        }

        return true;

    }


    /**
     * @param $goods_id
     * @功能说明:获取会员商品对类型
     * @author chenniang
     * @DataTime: 2020-09-14 09:31
     */
    public function getGoodsType($goods_id){

        $dis = [

            'a.goods_id' => $goods_id

        ];

        $data = $this->alias('a')
            ->join('longbing_card_member_level b','a.member_id = b.id')
            ->where($dis)
            ->field('b.*')
            ->find();

        return $data;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-07-29 15:31
     * @功能说明:支付回调
     */
    public function getGrowth($goods_id,$order_id,$uniacid,$user_id){

        $member_model = new Member();

        $growth_model = new Growth();

        $dis = [

            'a.goods_id' => $goods_id

        ];

        $growth = $this->alias('a')
                 ->join('longbing_card_member_level b','a.member_id = b.id')
                 ->where($dis)
                 ->value('growth');

        if(is_numeric($growth)&&$growth>0){

            $member_info = $member_model->getMemberInfo(['user_id'=>$user_id,'uniacid'=>$uniacid]);

            $insert = [

                'uniacid' => $uniacid,

                'user_id' => $user_id,

                'growth_add' => $growth,

                'growth_after'  => $member_info['growth']+$growth,

                'growth_before' => $member_info['growth'],

                'create_time'=> time(),

                'order_id'   => $order_id,

                'goods_id'   => $goods_id,

                'type'       => 0

            ];

            $growth_model->insert($insert);

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-14 10:19
     * @功能说明:更新会员
     */
    public function ordertypeUpdate($order){
        //普通会员
        if(!empty($order['level_top'])){

            $this->giveOrderGrowth($order);
        }
        //pul会员
        if(!empty($order['over_time'])){

            $member_model  = new Member();
            //获取用户会员信息(没有则创建)
            $user_info = $member_model->memberUpdateInfo(['user_id'=>$order['user_id'],'uniacid'=>$order['uniacid']]);
            //1是plus会员，给会员过期时间
            if($user_info['over_time']<time()){

                $time = strtotime("+".$order['over_time']."month");

            }else{

                $time = strtotime("+".$order['over_time']."month",$user_info['over_time']);

            }
            //永久的plus会员
            if($order['over_time']==-1){

                $time = -1;
            }

            $row['over_time'] = $time;

            $member_model->where(['id'=>$user_info['id']])->update($row);

        }
        return true;
    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-14 09:49
     * @功能说明:给会员对成长值
     */

    public function giveOrderGrowth($order){

        $growth_model  = new Growth();

        $level_model   = new Level();

        $member_model  = new Member();

        $order_goods_model = new IndexShopOrderGoods();

        $level_data = $level_model->where(['top'=>$order['level_top'],'uniacid'=>$order['uniacid']])->find();

        if(!empty($level_data)){

            $level_data = $level_data->toArray();
            //当前会员对信息
            $member_info = $member_model->getMemberInfo(['user_id'=>$order['user_id'],'uniacid'=>$order['uniacid']]);
            //子订单
            $order_item = $order_goods_model->orderGoodsInfo([ 'order_id' => $order[ 'id' ] ]);

            foreach ( $order_item as $index => $item ) {

                $insert = [

                    'uniacid' => $order['uniacid'],

                    'user_id' => $order['user_id'],

                    'growth_add' => $level_data['growth'],

                    'growth_after'  => $member_info['growth']+$level_data['growth'],

                    'growth_before' => $member_info['growth'],

                    'create_time'=> time(),

                    'order_id'   => $order['id'],

                    'goods_id'   => $item['goods_id'],

                    'type'       => 0

                ];

                $growth_model->insert($insert);


            }

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-30 09:41
     * @功能说明:获取可以买的商品
     */
    public function getCanBuyGoods($uid,$uniacid){

        $level_model = new Level();

        $level = $level_model->getUserLevel($uid,$uniacid);

        $member_model = new Member();
        //看他是不是永久的plus会员
        if(!empty($level)&&$level['top']==999){

            $over_time = $member_model->where(['user_id'=>$uid])->value('over_time');

            if($over_time==-1){

                return [];
            }

        }

        if(!empty($level)){

            $top_level = $level_model->getSonLevel($level,$uniacid,1);

        }else{

            $top_level = $level_model->getAllLevel($uniacid);

        }

        if(!empty($top_level)){

           $dis[] = ['a.member_id','in',$top_level];

           $dis[] = ['a.uniacid','=',$uniacid];

           $dis[] = ['b.is_member','=',1];

           $data  = $this->alias('a')
                    ->join('longbing_card_goods b','a.goods_id = b.id')
                    ->where($dis)
                    ->column('b.id');

        }

        return !empty($data)?$data:[];
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-30 13:48
     * @功能说明:获取商品的会员等级
     */
    public function getGoodsLevel($goods_id){

        $dis = [

            'a.goods_id' => $goods_id

        ];

        $data = $this->alias('a')
                ->join('longbing_card_member_level b','a.member_id = b.id')
                ->where($dis)
                ->field('b.*')
                ->find();

        return !empty($data)?$data->toArray():[];
    }
















}